#include "CCreditState.h"
#include "SGD Wrappers/CSGD_FModManager.h"
#include "SGD Wrappers/CSGD_TextureManager.h"
#include "SGD Wrappers/CSGD_DirectInput.h"
#include "CGame.h"
#include "CXInput.h"
#include "Mail System.h"
#include "CUnit.h"
#include "CObjectManager.h"
#include "CObjGenerator.h"
#include "CEventSystem.h"

CCreditState::CCreditState()
{
	Type(SUB);
	m_nBGImageID = CSGD_TextureManager::GetInstance()->LoadTexture("Resource/graphics/black.png");
	m_nImageID = CSGD_TextureManager::GetInstance()->LoadTexture("Resource/graphics/credits.png");
	m_nBGMusicID = CSGD_FModManager::GetInstance()->LoadSound("Resource/sound/Batallia Downs.mp3");
	m_nScrollSpeed = 1;
}

CCreditState::~CCreditState()
{

}

CCreditState* CCreditState::GetInstance()
{
	static CCreditState instance;
	return &instance;
}

void CCreditState::Load(char* szFilename)
{
	static char buffer[128];
	sprintf_s(buffer,_countof(buffer),"Resource/graphics/%s", szFilename);
	m_nImageID = CSGD_TextureManager::GetInstance()->LoadTexture(buffer);
}

void CCreditState::Enter()
{
	m_nPosY = 600;
	CObjectManager::GetInstance()->Clear();
}

void CCreditState::Exit()
{
}

void CCreditState::Update(float fDelta)
{
	Input();
	m_nPosY -= m_nScrollSpeed;
	if(m_nPosY <= - 800)
		CGame::GetInstance()->PopState();
}

bool CCreditState::Input()
{
	if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_SPACE))
		m_nScrollSpeed = 5;
	else
		m_nScrollSpeed = 1;

	if(CXInput::GetInstance()->IsConnected())
	{
		if(CXInput::GetInstance()->PressButton() && XINPUT_GAMEPAD_B)
			m_nScrollSpeed = 5;
		else
			m_nScrollSpeed = 1;
	}

	if(CSGD_DirectInput::GetInstance()->KeyPressedEx(DIK_ESCAPE)
		|| CXInput::GetInstance()->PressButton() && XINPUT_GAMEPAD_BACK)
		CGame::GetInstance()->PopState();
	return true;
}

void CCreditState::Render()
{
	CSGD_TextureManager::GetInstance()->Draw(m_nBGImageID, 0,0);

	CObjectManager::GetInstance()->Render();

	CSGD_TextureManager::GetInstance()->Draw(m_nImageID, 25, m_nPosY, .75f, .75f);
}